package com.kjzy.mapper;

import com.kjzy.pojo.Member;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

@Mapper
public interface MemberMapper {
    //增加
    @Insert("insert into `member`(name,address,grade,tel) values(#{name},#{address},#{grade},#{tel})")
    int createMember(Member member);
    //删除
    @Delete("delete from `member` where id=#{id}")
    int deleteMember(int id);
    //更改
    @Update("update `member` set grade=#{grade},name=#{name},address=#{address},tel=#{tel} where id=#{id}")
    int updateMember(Member member);
    //查找
    @Select("select * from `member` where id=#{id}")
    Member findById(int id);
    @SelectProvider(type = MemberSqlBuilder.class,method = "buildFindAllM")
    List<Member> findAllM(Member member);
    class MemberSqlBuilder{
        public static String buildFindAllM(final Member member){
            return new SQL(){{
                SELECT("*");
                FROM("`member`");
                if (member.getId()!=null && !member.getId().equals("")){
                    WHERE("id=#{id}");
                }
                if (member.getName()!=null && !member.getName().equals("")){
                    WHERE("name LIKE CONCAT('%', #{name}, '%')");
                }
                if (member.getGrade()!=null && !member.getGrade().equals("")){
                    WHERE("grade LIKE CONCAT('%', #{grade}, '%')");
                }
            }}.toString();
        }
    }
}
